
import NewsAPI from '../api/news.js';

export const News={
   state:{
      newslist:[],
      news_total_count:0,
      editNewsStatus:-1,     //插入状态 0失败  1成功  -1不改变
      newsinfo:[],
      delNewsStatus:-1,       //删除状态
      allnewstype:[           //公告发布位置
        { value:1 ,label:'学习平台'},
        { value:2 , label:'首页-街道简报'},
        { value:3 , label:'首页-社区动态'},
        { value:4 , label:'服务-民生政策'},
        { value:5 , label:'服务-在线说事'},
        { value:6 , label:'治理-疫情防控'},
        { value:7 , label:'治理-普法宣传'},
        { value:8 , label:'治理-扫黑除恶'},
        { value:9 , label:'服务-办事指南'},
      ],
      //筛选条件
      search_news_type:''
   },
   mutations:{
      setNewsList(state,list){
          state.newslist=list;
      },
      setNewsTotalCount(state,total_count){
          state.news_total_count=total_count;
      },
      setEditNewsStatus(state,status){
          state.editNewsStatus=status;
      },
      setNewsInfo(state,info){
          state.newsinfo=info;
      },
      setDelNewsStatus(state,status){
          state.delNewsStatus=status;
      },
      setAllNewsType(state,list){
          state.allnewstype=list;
      },
      setSearchNewsType(state,type){
          state.search_news_type=type;
      }
   },
   actions:{
      loadNewsList(context,data){
          NewsAPI.getNewsList(data).then(function(response){
              if(response.data.code==1){
                  context.commit('setNewsList',response.data.list);
                  context.commit('setNewsTotalCount',response.data.total_count);
              }
          })
      },
      //插入news
      editNews(context,data){
          NewsAPI.editNews(data).then(function(response){
              context.commit('setEditNewsStatus',response.data.code);
          })
      },
      //获取详情
      loadNewsInfo(context,data){
          NewsAPI.getNewsInfo(data).then(function(response){
              if(response.data.code==1){
                  context.commit('setNewsInfo',response.data.info);
              }
          })
      },
      //删除
      delNews(context,data){
          NewsAPI.delNews(data).then(function(response){
                context.commit('setDelNewsStatus',response.data.code);
          })
      },
      loadAllNewsType(context,data){
          NewsAPI.getAllNewsType(data).then(function(response){
                context.commit('setAllNewsType',response.data.list);
          })
      }
   },
   getters:{
      getNewsList:state =>{
         return state.newslist
      },
      getNewsTotalCount: state =>{
         return state.news_total_count
      },
      getEditNewsStatus:state=>{
          return state.editNewsStatus;
      },
      getNewsInfo:state => {
          return state.newsinfo;
      },
      getDelNewsStatus:state => {
          return state.delNewsStatus;
      },
      getAllNewsType:state => {
          return state.allnewstype;
      },
      getSearchNewsTye:state => {
          return state.search_news_type;
      }
   }
}
